import sqlite3
from models import create_user, create_role, create_permission, create_menu, get_user_by_username

# 创建测试用户
print("Creating test users...")

# 检查用户是否已存在
try:
    admin_id = create_user('admin', 'admin123', 'admin@example.com', '管理员')
    print(f"Created admin user with ID: {admin_id}")
except sqlite3.IntegrityError:
    print("Admin user already exists")

try:
    user_id = create_user('user', 'user123', 'user@example.com', '普通用户')
    print(f"Created regular user with ID: {user_id}")
except sqlite3.IntegrityError:
    print("User already exists")

# 创建测试角色
print("Creating test roles...")
try:
    admin_role_id = create_role('管理员', '系统管理员角色')
    print(f"Created admin role with ID: {admin_role_id}")
except sqlite3.IntegrityError:
    print("Admin role already exists")

try:
    user_role_id = create_role('用户', '普通用户角色')
    print(f"Created user role with ID: {user_role_id}")
except sqlite3.IntegrityError:
    print("User role already exists")

# 创建测试权限
print("Creating test permissions...")
try:
    user_manage_perm_id = create_permission('用户管理', 'user.manage', '管理用户信息', '系统管理')
    print(f"Created user management permission with ID: {user_manage_perm_id}")
except sqlite3.IntegrityError:
    print("User management permission already exists")

try:
    role_manage_perm_id = create_permission('角色管理', 'role.manage', '管理角色信息', '系统管理')
    print(f"Created role management permission with ID: {role_manage_perm_id}")
except sqlite3.IntegrityError:
    print("Role management permission already exists")

try:
    perm_manage_perm_id = create_permission('权限管理', 'permission.manage', '管理权限信息', '系统管理')
    print(f"Created permission management permission with ID: {perm_manage_perm_id}")
except sqlite3.IntegrityError:
    print("Permission management permission already exists")

try:
    menu_manage_perm_id = create_permission('菜单管理', 'menu.manage', '管理菜单信息', '系统管理')
    print(f"Created menu management permission with ID: {menu_manage_perm_id}")
except sqlite3.IntegrityError:
    print("Menu management permission already exists")

# 创建测试菜单
print("Creating test menus...")
try:
    dashboard_menu_id = create_menu('仪表盘', 0, 'fas fa-home', '/', None, 1, 1, None)
    print(f"Created dashboard menu with ID: {dashboard_menu_id}")
except sqlite3.IntegrityError:
    print("Dashboard menu already exists")

try:
    system_menu_id = create_menu('系统管理', 0, 'fas fa-cog', None, None, 2, 1, None)
    print(f"Created system management menu with ID: {system_menu_id}")
except sqlite3.IntegrityError:
    print("System management menu already exists")

try:
    user_menu_id = create_menu('用户管理', system_menu_id, 'fas fa-users', '/users', None, 1, 1, user_manage_perm_id)
    print(f"Created user management menu with ID: {user_menu_id}")
except sqlite3.IntegrityError:
    print("User management menu already exists")

try:
    role_menu_id = create_menu('角色管理', system_menu_id, 'fas fa-user-tag', '/roles', None, 2, 1, role_manage_perm_id)
    print(f"Created role management menu with ID: {role_menu_id}")
except sqlite3.IntegrityError:
    print("Role management menu already exists")

try:
    perm_menu_id = create_menu('权限管理', system_menu_id, 'fas fa-key', '/permissions', None, 3, 1, perm_manage_perm_id)
    print(f"Created permission management menu with ID: {perm_menu_id}")
except sqlite3.IntegrityError:
    print("Permission management menu already exists")

try:
    menu_menu_id = create_menu('菜单管理', system_menu_id, 'fas fa-bars', '/menus', None, 4, 1, menu_manage_perm_id)
    print(f"Created menu management menu with ID: {menu_menu_id}")
except sqlite3.IntegrityError:
    print("Menu management menu already exists")

print("All test data creation process completed!")